
package com.nikmesoft.android.alwaysontime.activities;

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.nikmesoft.android.alwaysontime.R;
import com.nikmesoft.android.alwaysontime.adapters.SmsMainAdapter;
import com.nikmesoft.android.alwaysontime.binding.ExtraBinding;
import com.nikmesoft.android.alwaysontime.daos.SmsMainDAO;
import com.nikmesoft.android.alwaysontime.models.SmsMainItem;

public class SMSActivity extends BaseActivity implements OnItemClickListener
{	ListView smsMainList;
	SmsMainAdapter smsAdapter;
	ArrayList<SmsMainItem> smsList;
	Spinner spinner;
	ArrayAdapter<CharSequence> spinnerAdapter;
	// database
	SmsMainDAO smsDAO;
	/*static final int ALL_SMS = 0;
	static final int ONETIME_SMS = 1;
	static final int SCHEDLE_SMS = 2;*/
	enum SMS_TYPE { all, oneTime, Schedule}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{	super.onCreate(savedInstanceState);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_sms, null);
		this.setContentView(viewToLoad);
		// setContentView(R.layout.activity_sms);
		init();
	}

	private void init()
	{ // database va listview
		smsDAO = SmsMainDAO.getInstance();
		smsMainList = (ListView) findViewById(R.id.smsMainListView);
		// tao list
		smsList = new ArrayList<SmsMainItem>();
		getDataForSmsList();
		// SmsMainAdapter = new
		smsAdapter = new SmsMainAdapter(this, R.layout.sms_list_item_main, smsList);
		smsMainList.setAdapter(smsAdapter);
		smsMainList.setOnItemClickListener(this);
		// nap du lieu cho spinner
		spinner = (Spinner) findViewById(R.id.smsMainSpinner);
		getDataForSpinner();
		setSpinnerListenner();
		
		// add listenner for Add and Sent button
		addListennerForButton();
	}

	public void getDataForSmsList()
	{ // ket noi database
		smsList = smsDAO.getSms(SMS_TYPE.all.ordinal());
	}

	public void getDataForSpinner()
	{	spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.smsMainSpinnerString, android.R.layout.simple_spinner_item);
		spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerAdapter);
	}

	// ========= xu li cho spinner=============
	// dat listenner cho spinner de xu li khi chon
	public void setSpinnerListenner()
	{	spinner.setOnItemSelectedListener(new OnItemSelectedListener()
		{	@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
			{	//Log.i("Spinner click tai vi tri  : ", "id spinner = " + id);
				//Log.i("Spinner click tai position  : ", "position spinner = " + position);
				switch (position)
				{	case 0:// all sms
						smsList = smsDAO.getSms(SMS_TYPE.all.ordinal());
						break;
					case 1:// oneTime
						smsList = smsDAO.getSms(SMS_TYPE.oneTime.ordinal());
						break;
					case 2:// Schedule
						smsList = smsDAO.getSms(SMS_TYPE.Schedule.ordinal());
						break;
				}
				// sau khi chon xong, reload sms list
				reloadSmsList();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{}
		});
	}
	
	public void reloadSmsList()
	{	smsAdapter.clear();
		for (int i = 0; i < smsList.size(); i++)
			smsAdapter.add(smsList.get(i));
		smsAdapter.notifyDataSetChanged();
	}
	
	public void readSmsDatabase()
	{	switch (spinner.getSelectedItemPosition())
		{	case 0:// all sms
				smsList = smsDAO.getSms(SMS_TYPE.all.ordinal());
				break;
			case 1:// oneTime
				smsList = smsDAO.getSms(SMS_TYPE.oneTime.ordinal());
				break;
			case 2:// Schedule
				smsList = smsDAO.getSms(SMS_TYPE.Schedule.ordinal());
				break;
		}
	}

	// -------------click item on ListView-----------
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
	{	SMSGroup parent = (SMSGroup) getParent();
		Intent intent = new Intent(parent, SMSActivityAdd.class);
		// getItem
		SmsMainItem item = smsAdapter.getItem(position);
		// cho 1 bien integer de smsAdd bien la add hay la edit
		intent.putExtra("AddOrEdit", "edit");
		// ko can get over, vi trong listView da loc ra nhung item co over = true
		Bundle bundle = new Bundle();
		bundle.putSerializable("sms", item);
		intent.putExtras(bundle);
		//parent.startActivityForResult(intent, 100);
		parent.startChildActivity(SMSActivityAdd.class.getSimpleName(), intent);
	}

	@Override
	public void onGroupActivityResult(int requestCode, int resultCode, Intent data)
	{	super.onGroupActivityResult(requestCode, resultCode, data);
		if (requestCode == 100)
		{	if (resultCode == RESULT_OK)
			{	
			}
			else if (resultCode == RESULT_CANCELED)
			{	
			}
		}
	}
	
	//========= listenner for Add button and Sent button
	Button btSmsMainAdd, btSmsMainSent;
	public void addListennerForButton()
	{	// connect to xml
		btSmsMainAdd = (Button) findViewById( R.id.btSmsMainAdd);
		btSmsMainSent = ( Button) findViewById( R.id.btSmsMainSent);
		// add listenner
		btSmsMainAdd.setOnClickListener( new OnClickListener()
		{	public void onClick(View arg0)
			{	
				SMSGroup parent = (SMSGroup) getParent();
				Intent intent = new Intent(parent, SMSActivityAdd.class);
				intent.putExtra("AddOrEdit", "");
				parent.startChildActivity(SMSActivityAdd.class.getSimpleName(), intent);
				//parent.startActivityForResult(intent, 100);

			}
		});
		//sent listener
		btSmsMainSent.setOnClickListener( new OnClickListener()
		{	public void onClick(View arg0)
			{	SMSGroup parent = (SMSGroup) getParent();
				Intent intent = new Intent(parent, SMSActivitySent.class);
				parent.startChildActivity(SMSActivitySent.class.getSimpleName(), intent);
				//parent.startActivityForResult(intent, 100);
			}
		});
	}
}
